﻿@page "/logicdeftable"
@attribute [TabItemOption(Text = "逻辑表达式列表")]

@using BootstrapBlazor.Components

<h3>逻辑表达式列表</h3>

<Divider></Divider>
<table class="table table-users">
    <thead>
        <tr>
            <th scope="col" style="width: 50px">ID</th>
            <th scope="col" style="width: 100px">名称</th>
            <th scope="col" style="width: 200px">描述</th>
            <th scope="col" style="width: 200px">条件表达式</th>
            <th scope="col" style="width: 300px">内容</th>
            <th scope="col" style="width: 100px">使能</th>
            <th scope="col" style="width: 100px">更新方式</th>
            <th scope="col" style="width: 100px">设备</th>
            <th scope="col" style="width: 200px">操作</th>
        </tr>
    </thead>

    <tbody>
        @if (logicDtos.Any())
        {
            @foreach (var item in logicDtos)
            {
                @if (dictChanges[item.Id])
                {
                    <tr>
                        <td class="align-middle">@item.Id</td>
                        <td class="align-middle">@item.Name</td>
                        <td class="align-middle">@item.Description</td>
                        <td class="align-middle">@item.ConditionFormula</td>
                        <td class="align-middle">@item.ClacateFormula</td>
                        <td class="align-middle">
                            <Switch IsDisabled="true" OnText="使能" OffText="取消" @bind-Value="@item.Enabled" DisplayText="初始化启动" />
                        </td>
                        <td class="align-middle">
                            <Switch IsDisabled="true" OnText="改变" OffText="持续" @bind-Value="@item.UpdateWhenChanged" DisplayText="刷新方式" />
                        </td>
                        <td class="align-middle">@item.Equip.Name</td>
                        <td class="align-middle">
                            <Button class="btn btn-info" OnClick="@(() => OnSwitchToChanged(item.Id))"><i class="fa-solid fa-hammer"></i></Button>
                            <PopConfirmButton Placement="Placement.Top"
                                                Color="Color.Danger"
                                                ConfirmIcon="fa-solid fa-triangle-exclamation text-danger"
                                                ConfirmButtonColor="Color.Danger"
                                                Text=""
                                                Content="确定删除数据吗？"
                                                OnConfirm="@(() => OnDeleteLogic(item.Id))">
                                <i class="fa-solid fa-delete-left"></i>
                            </PopConfirmButton>
                        </td>
                    </tr>
                }
                else
                {
                    <tr>
                        <td class="align-middle"></td>
                        <td class="align-middle"></td>
                        <td class="align-middle"></td>
                        <td class="align-middle">
                            <Dropdown
                                TValue="string"
                                Items="NameItems"
                                OnSelectedItemChanged="@((a) => OnCondRegNameChanged(a, item.Id))"
                                Color="Color.Secondary">
                            </Dropdown>
                        </td>
                        <td class="align-middle">
                            <Dropdown
                                TValue="string"
                                Items="NameItems"
                                OnSelectedItemChanged="@((a) => OnClacRegNameChanged(a, item.Id))"
                                Color="Color.Secondary">
                            </Dropdown>
                        </td>
                        <td class="align-middle"></td>
                        <td class="align-middle"></td>
                        <td class="align-middle"></td>
                        <td class="align-middle"></td>
                    </tr>
                    <tr>
                        <td class="align-middle">@item.Id</td>
                        <td class="align-middle">
                            <BootstrapInput @bind-value="@item.Name" />
                        </td>
                        <td class="align-middle">
                            <BootstrapInput @bind-value="@item.Description" />

                        </td>
                        <td class="align-middle">
                            @{
                                string cond_id = $"cond{item.Id}";
                            }
                            <BootstrapInput id="@cond_id" @OnBlurAsync="@(() => OnCondClick(item.Id))" @bind-Value="@item.ConditionFormula" />
                        </td>
                        <td class="align-middle">
                            @{
                                string calc_id = $"calc{item.Id}";
                            }
                            <Textarea id="@calc_id" row="2" @OnBlurAsync="@(() => OnCalcClick(item.Id))" @bind-Value="@item.ClacateFormula" />
                        </td>
                        <td class="align-middle">
                            <Switch OnText="使能" OffText="取消" @bind-Value="@item.Enabled" DisplayText="初始化启动" />
                        </td>
                        <td class="align-middle">
                            <Switch OnText="持续" OffText="改变" @bind-Value="@item.UpdateWhenChanged" DisplayText="刷新方式" />
                        </td>
                        <td class="align-middle">
                            <Dropdown TValue="int" Items="equipList" Color="Color.Secondary" @bind-Value="@item.equipId"></Dropdown>
                        </td>
                        <td class="align-middle">
                            <Button class="btn btn-primary" OnClick="@(() => OnAcceptChanged(item))">保存</Button>
                            <Button class="btn btn-secondary" OnClick="@(() => OnRefuseChanged(item))">取消</Button>
                        </td>
                    </tr>

                }
            }
        }

        @if (bInsertFlag)
        {
            <tr>
                <td class="align-middle"></td>
                <td class="align-middle"></td>
                <td class="align-middle"></td>
                <td class="align-middle">
                    <Dropdown
                        TValue="string"
                        Items="NameItems"
                        OnSelectedItemChanged="@((a) => OnCondRegNameChanged(a, 0))"
                        Color="Color.Secondary">
                    </Dropdown>
                </td>
                <td class="align-middle">
                    <Dropdown
                        TValue="string"
                        Items="NameItems"
                        OnSelectedItemChanged="@((a) => OnClacRegNameChanged(a, 0))"
                        Color="Color.Secondary">
                    </Dropdown>
                </td>
                <td class="align-middle"></td>
                <td class="align-middle"></td>
                <td class="align-middle"></td>
                <td class="align-middle"></td>
            </tr>
            <tr>
                <td class="align-middle"></td>
                <td class="align-middle">
                    <BootstrapInput @bind-value="@addedLogic.Name" />
                </td>
                <td class="align-middle">
                    <BootstrapInput @bind-value="@addedLogic.Description" />

                </td>
                <td class="align-middle">
                    <BootstrapInput id="cond0" @OnBlurAsync="@(() => OnCondClick(0))" @bind-Value="@addedLogic.ConditionFormula" />
                </td>
                <td class="align-middle">
                    <Textarea id="calc0" row="2" @OnBlurAsync="@(() => OnCalcClick(0))" @bind-Value="@addedLogic.ClacateFormula" />
                </td>
                <td class="align-middle">
                    <Switch OnText="使能" OffText="取消" @bind-Value="@addedLogic.Enabled" DisplayText="初始化启动" />
                </td>
                <td class="align-middle">
                    <Switch  OnText="持续" OffText="改变" @bind-Value="@addedLogic.UpdateWhenChanged" DisplayText="刷新方式" />
                </td>
                <td class="align-middle">
                    <Dropdown TValue="int" Items="equipList" Color="Color.Secondary" @bind-Value="@addedLogic.equipId"></Dropdown>
                </td>
                <td class="align-middle">
                    <Button class="btn btn-primary"     OnClick="OnAcceptInsert">保存</Button>
                    <Button class="btn btn-secondary"   OnClick="OnRefuseInsert">取消</Button>
                </td>
            </tr>
        }

    </tbody>

</table>

<br/>
@if (!bInsertFlag)
{
    <Button type="button" class="btn btn-secondary" OnClick="OnSwitchToInsert">新增</Button>
}
<br />

<Divider></Divider>
<Pagination PageCount="@MaxPageNumber" Alignment="Alignment.Center" class="mt-3" OnPageLinkClick="@OnPageClick" >
    <PageInfoTemplate>
        <div class="page-info me-2">@PageInfoText</div>
        <Select @bind-Value="PageItems" Items="PageItemsSource" OnSelectedItemChanged="@OnPageItemsChanged"></Select>
    </PageInfoTemplate>
</Pagination>

<p>@sMsg</p>


<style>
    /* 添加下拉菜单容器滚动条 */
    .dropdown-menu {
        max-height: 200px; /* 限制最大高度 */
        overflow-y: auto; /* 垂直滚动条 */
        scrollbar-width: thin; /* 细滚动条样式 */
    }

        /* 滚动条美化（可选）*/
        .dropdown-menu::-webkit-scrollbar {
            width: 6px;
        }

        .dropdown-menu::-webkit-scrollbar-thumb {
            background: #888;
            border-radius: 3px;
        }
</style>
